import { Component, OnInit } from '@angular/core'
import { JsonApiService } from 'app/core/api/json-api.service' //http请求配置引入
import { Router, ActivatedRoute } from '@angular/router' //路由引入
import { config } from '../../shared/smartadmin.config'
import { C } from '@angular/core/src/render3'

declare var $: any
declare var echarts: any
declare var Viewer: any
@Component({
  selector: 'sa-host-report',
  templateUrl: './host-report.component.html',
  styleUrls: ['./host-report.component.css']
})
export class HostReportComponent implements OnInit {
  tableData = [] //table数据
  totalCount: number = 0 //分页总条数
  curPage: number = 1 //分页当前页
  pageSize: number = 10 //内置的分页每页条数
  searchKey: string = '' //搜索keyword
  startTime: string = ''
  endTime: string = ''
  faultPhenomenon: string = ''
  formType: string = 'new'

  isShowAdd: boolean = false
  isShowDel: boolean = false
  isShowEdit: boolean = false

  tableSearch: any = {
    resultStatus: '' //检验结果
  }

  constructor(private jsonApiService: JsonApiService, private router: Router, private activatedRoute: ActivatedRoute) {
    activatedRoute.queryParams.subscribe(queryParams => {})
  }

  ngOnInit() {
    System.import('assets/js/datepicker/js/bootstrap-datetimepicker.min.js').then(() => {
      System.import('assets/js/datepicker/locales/bootstrap-datetimepicker.zh-CN.js').then(() => {
        this.initDate()
        $('#datetimepicker1')
          .datetimepicker({
            format: 'yyyy-mm-dd',
            language: 'zh-CN',
            autoclose: true,
            minView: 2
          })
          .on('changeDate', function (e) {
            var date = e.date
            var time = common.getTimeDay(date)
            this.startTime = time
            $('#datetimepicker2').datetimepicker('setStartDate', time)
          })
        $('#datetimepicker2')
          .datetimepicker({
            format: 'yyyy-mm-dd',
            language: 'zh-CN',
            autoclose: true,
            minView: 2
          })
          .on('changeDate', function (e) {
            var date = e.date
            var time = common.getTimeDay(date)
            this.endTime = time
            $('#datetimepicker1').datetimepicker('setEndDate', time)
          })
      })
    })
    this.load()
  }

  initDate() {
    // common.initDatePicker('#checkDate', 1)
    // common.initDatePicker('#inspectDateAdd', 1)
    // let nowTime = new Date().toLocaleDateString().replace(/\//g,'-');
    // $('#inspectDateAdd').val(nowTime) //当前时间
  }

  //删除所选时间
  del_date() {
    this.startTime = ''
    this.endTime = ''
    $('#datetimepicker1').val('').change()
    $('#datetimepicker2').val('').change()
    $('#datetimepicker2').datetimepicker('setStartDate', null)
    $('#datetimepicker1').datetimepicker('setEndDate', null)
  }
  //table刷新
  refresh() {
    this.load()
  }
  //表格搜索
  table_search() {
    this.curPage = 1
    this.searchKey = common.stringTrim(this.searchKey)
    this.load()
  }
  //初始化加载list数据
  load() {
    this.startTime = $('#datetimepicker1').val()
    this.endTime = $('#datetimepicker2').val()
    let data = {
      pageSize: this.pageSize,
      nowPage: this.curPage,
      parameters: {
        startTime: this.startTime ? this.startTime + ' 00:00:00' : '',
        endTime: this.endTime ? this.endTime + ' 00:00:00' : '',
        ...this.tableSearch
      }
    }
    let reqdata = 'gridPager=' + JSON.stringify(data)
    this.jsonApiService.http('hostReport/hostReportPage.html', reqdata).subscribe(data => {
      this.tableData = data.exhibitDatas || []
      this.totalCount = data.recordCount
    })
  }

  //分页回调
  paginate(e) {
    this.curPage = e.page + 1
    this.pageSize = e.rows
    this.load()
  }

  detailRow(row){
    const BaseURL = window.location.protocol + '//' + window.location.hostname + ':' + window.location.port;
		common.setCookie('hostReportDetail', JSON.stringify(row));
		window.open(BaseURL + '#/supply-chain/host-report-detail');
  }

  deleteRow(row) {
    this.jsonApiService.confirm_message('将删除本条记录包含的所有主机检验明细，是否确定删除？').then(() => {
      let reqdata = 'ids=' + row.id
      this.jsonApiService.http('hostReport/deleteHostReportById.html', reqdata).subscribe(data => {
        this.jsonApiService.message(data.message)
        if (data.success) {
          this.load()
        }
      })
    })
  }
}
